package de.hpi.generator.model.dependencies;

import de.hpi.generator.config.UserInputException;
import de.hpi.generator.model.Column;
import de.hpi.generator.model.TesmaColumn;

public class UnaryInclusionDependency extends InclusionDependency {

    public UnaryInclusionDependency(Column aDependent, Column aReferenced) throws Exception {
        super(aDependent, aReferenced);
        validate();
    }

    @Override
    public String toString() {
        return aDependent.getOwner().getName() + ":" + aDependent.getName() + " => " + aReferenced.getOwner().getName() + ":" + aReferenced.getName();
    }

    @Override
    public void generateDependentData() throws Exception {
        System.out.println("Generating dependent data for " + this);

        Column aDependentColumn = this.getDependent();
        if (aDependentColumn.isValuesNotGenerated()) {
            aDependentColumn.generateBaseColumnValues();
        }
    }

    protected void validate() throws Exception {

        // Datatypes of LHS  and RHS  should be matched
        if (aDependent.getDataType().compareTo(TesmaColumn.TYPE_NAME) != 0
                && aReferenced.getDataType().compareToIgnoreCase(aDependent.getDataType()) != 0) {

            throw new UserInputException("The datatype of " + aDependent.getOwner().getName() + ":" + aDependent.getName()
                    + " " + aDependent.getDataType() + " is mismatched with datatype of "
                    + aReferenced.getOwner().getName() + ":" + aReferenced.getName() + " " + aReferenced.getDataType());

        }
        // Warning: Size of RHS  should be not smaller than of LHS one
        if (aReferenced.getCellSize() < aDependent.getCellSize()) {

            throw new UserInputException("The cell size of " + aReferenced.getOwner().getName() + ":" + aReferenced.getName()
                    + " " + aReferenced.getCellSize() + " should be not smaller than the cell size of "
                    + aDependent.getOwner().getName() + ":" + aDependent.getName() + " " + aDependent.getCellSize());

        }
    }
}
